package com.lixixi.tingshu.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lixixi.tingshu.common.util.AuthContextHolder;
import com.lixixi.tingshu.model.user.UserPaidTrack;
import com.lixixi.tingshu.user.mapper.UserPaidTrackMapper;
import com.lixixi.tingshu.user.service.UserPaidTrackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserPaidTrackServiceImpl extends ServiceImpl<UserPaidTrackMapper, UserPaidTrack> implements UserPaidTrackService {
	@Autowired
	UserPaidTrackMapper userPaidTrackMapper;

	@Override
	public Map<Long, Integer> judgeUserIsPaidTrack(Long albumId, List<Long> trackIdList) {
		//获取用户id
		Long userId = AuthContextHolder.getUserId();
		//构建查询条件
		LambdaQueryWrapper<UserPaidTrack> userPaidTrackLambdaQueryWrapper = new LambdaQueryWrapper<>();
		userPaidTrackLambdaQueryWrapper.eq(UserPaidTrack::getUserId, userId)
				.eq(UserPaidTrack::getAlbumId, albumId)
				.in(UserPaidTrack::getAlbumId, trackIdList);
		//获取用户在专辑下购买的声音列表
		List<UserPaidTrack> userPaidTrackList = this.list(userPaidTrackLambdaQueryWrapper);
		HashMap<Long, Integer> resultMap = new HashMap<>();
		if (!CollectionUtils.isEmpty(userPaidTrackList)){
			//列表不为空,将专辑购买声音整理
			//key为声音id, value为购买标识
			userPaidTrackList.forEach(userPaidTrack -> {
				resultMap.put(userPaidTrack.getTrackId(), 1);
			});
		}
		return resultMap;
	}

	@Override
	public List<Long> getPaidTrackIdList(Long albumId) {
		//获取用户id
		Long userId = AuthContextHolder.getUserId();
		//构建查询条件
		LambdaQueryWrapper<UserPaidTrack> userPaidTrackLambdaQueryWrapper = new LambdaQueryWrapper<>();
		userPaidTrackLambdaQueryWrapper.eq(UserPaidTrack::getUserId, userId)
				.eq(UserPaidTrack::getAlbumId, albumId);
		//获取用户在专辑下购买的声音列表
		List<UserPaidTrack> userPaidTrackList = this.list(userPaidTrackLambdaQueryWrapper);
		return userPaidTrackList.stream().map(UserPaidTrack::getTrackId).toList();
	}
}
